import pandas as pd
from openpyxl import load_workbook
import os
import openpyxl.utils
from PIL import Image
import io

def return_headers(account_id=None):
    return {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "content-type": "application/json",
        "origin": "https://ad.xiaohongshu.com",
        "priority": "u=1, i",
        "referer": f"https://ad.xiaohongshu.com/asset/schemeAssetManage?vSellerId={account_id}",
        "sec-ch-ua": "\"Google Chrome\";v=\"137\", \"Chromium\";v=\"137\", \"Not/A)Brand\";v=\"24\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36",
        "v-seller-id": f"{account_id}",
        "x-b3-traceid": "12d70d203e516e18"
}

mock_click_js = """
function triggerFullClick(element) {
  // 1. 触发鼠标按下
  element.dispatchEvent(new MouseEvent('mousedown', {
    bubbles: true,
    cancelable: true,
    view: window
  }));

  // 2. 触发获取焦点
  element.focus();
  element.dispatchEvent(new Event('focus', {bubbles: true}));

  // 3. 触发鼠标释放
  element.dispatchEvent(new MouseEvent('mouseup', {
    bubbles: true,
    view: window
  }));

  // 4. 触发最终点击
  element.dispatchEvent(new MouseEvent('click', {
    bubbles: true,
    view: window
  }));
}

// 执行函数
triggerFullClick(this);
"""
# 设置显示所有列（默认只显示部分列）
pd.set_option('display.max_columns', None)

# 设置显示所有行（默认只显示部分行）
pd.set_option('display.max_rows', None)

# 设置列宽（防止内容被截断）
pd.set_option('display.max_colwidth', 100)  # 设置每列最大显示宽度

def get_complete_sheet1(file_path=None):
    """
    读取直达链接预先录入sheet，若不存在返回空DataFrame
    """
    try:
        df = pd.read_excel(file_path)
        return df
    except ValueError as e:
        if "Worksheet named '直达链接预先录入' not found" in str(e):
            print(f"警告: 文件 {file_path} 中不存在[直达链接预先录入]sheet")
            return pd.DataFrame()  # 返回空DF
        else:
            raise
    except Exception as e:
        print(f"读取文件失败: {e}")
        return pd.DataFrame()

